﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Model_SSResourceManager;

namespace SSRMmonitor
{
    class Monitor
    {
        private Timer m_timer = new Timer(3000);
        private ModelsManager m_manager ;
        private bool inProcess = false;


        public Monitor(ModelsManager _manager)
        {
            m_manager = _manager;
            m_timer.Elapsed += new ElapsedEventHandler(RunMonitor);
            m_timer.Start();
        }

        private void RunMonitor(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!this.inProcess)
            {
                inProcess = true;
                CheckQueues();
                CheckMessages();
                inProcess = false;
            }
        }

        private void CheckMessages()
        {
            List<Message> _msgList = m_manager.messageModel.GetAll(true);

            foreach (Message msg in _msgList)
                m_manager.messageModel.SendMessage(msg);
        }

        private void CheckQueues()
        {
            IEnumerable<Queue> _queueList = 
                m_manager.queueModel.GetAll(true).
                Where(queue => queue.MessageTime != null && queue.MessageTime <= DateTime.Now);

            Message _msg;
            User _user;
            String userName;                      
            
            foreach (Queue _queue in _queueList)
            {
                _user = m_manager.userModel.Get(_queue.IdUser);
                if (_user.FirstName.Trim() != String.Empty)
                    userName = _user.FirstName;
                else
                    userName = _user.Login;
               
                _msg=m_manager.messageModel.Create(_queue.IdUser, 
                                                    RemindMessageTxt(   userName ,
                                                                        m_manager.resourceModel.Get(_queue.IdResource).Name ,
                                                                        _queue.StartTime 
                                                                    )
                                                  );
                if ( m_manager.messageModel.Add(_msg) )
                {
                    _queue.MessageTime = null;
                    if ( !m_manager.queueModel.Update(_queue) )
                    {
                        // must be event
                    }
                }
            }
        }

        private string RemindMessageTxt(string userName, string objectName, DateTime startTime)
        {
            StringBuilder MessageTxt = new StringBuilder();
            MessageTxt.AppendFormat("Dear {0}, your turn to {1} comes at: {2}",
                userName.Trim(),
                objectName.Trim(),
                startTime.ToShortTimeString());

            return (MessageTxt.ToString());
        }

    }
}
